package com.github.freewu.commons.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.DecimalFormat;

/**
 * 常用工具类
 * 
 * @author Hoyn
 * 
 */
public class Util {

	/**
	 * 保留2位小数
	 * 
	 * @param num
	 * @return
	 */
	public static float floatFormat(float num) {
		DecimalFormat decimalFormat = new DecimalFormat(".00");// 构造方法的字符格式这里如果小数不足2位,会以0补足.
		return Float.parseFloat(decimalFormat.format(num));// format 返回的是字符串
	}

	/**
	 * 字符串转为utf-8
	 * 
	 * @param content
	 * @return
	 */
	public static String toUtf_8(String content) {
		byte[] bytes = null;
		try {
			bytes = content.getBytes("UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return URLEncoder.encode(new String(bytes));
	}

	/**
	 * 字符全角化，解决textview换行排版问题
	 * 
	 * @param input
	 * @return
	 */
	public static String ToDBC(String input) {
		char[] c = input.toCharArray();
		for (int i = 0; i < c.length; i++) {
			if (c[i] == 12288) {
				c[i] = (char) 32;
				continue;
			}
			if (c[i] > 65280 && c[i] < 65375)
				c[i] = (char) (c[i] - 65248);
		}
		return new String(c);
	}
}
